<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\UserPassageRegister;

class UserPassageRegisterRepository
{
    /**
     * 创建用户对应通道是否开卡
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return UserPassageRegister::create($params);
    }

    public function isOpenCard($passageId, $userId, $cardId)
    {
        return UserPassageRegister::where([
            'passage_id' => $passageId,
            'user_id' => $userId,
            'card_id' => $cardId,
        ])->first();
    }

    public function isBindCard($passageId, $userId, $cardId)
    {
        return UserPassageRegister::where([
            'passage_id' => $passageId,
            'user_id' => $userId,
            'card_id' => $cardId,
            'is_bind' => 1,
        ])->first();
    }

    public function isNeedCreate($passageId, $userId, $cardId)
    {
        return UserPassageRegister::where([
            'passage_id' => $passageId,
            'user_id' => $userId,
            'card_id' => $cardId,
        ])->first();
    }

    public function updateOpenCardStatus($orderNo, $status)
    {
        return UserPassageRegister::where([
            'order_no' => $orderNo,
        ])->update(['is_open' => $status]);
    }

    public function orderNoByStatus($orderNo)
    {
        return UserPassageRegister::where([
            'order_no' => $orderNo,
        ])->first();
    }

    public function updateOpenCardOrderNo($passageId, $userId, $cardId, $orderNo)
    {
        return UserPassageRegister::where([
            'passage_id' => $passageId,
            'user_id' => $userId,
            'card_id' => $cardId,
        ])->update(['order_no' => $orderNo]);
    }

    public function updateOpenCardToCreditCard($passageId, $userId, $cardId, $status)
    {
        return UserPassageRegister::where([
            'passage_id' => $passageId,
            'user_id' => $userId,
            'card_id' => $cardId,
        ])->update(['is_open' => $status]);
    }
}